package com.ftg.learn.bus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author :[魏英豪]
 * @date :[2022/3/8 8:51]
 */
@CrossOrigin(origins = "http://26.234.93.45",
        methods = {RequestMethod.GET, RequestMethod.POST,
                RequestMethod.DELETE, RequestMethod.PUT},
        allowedHeaders = "*")
public class CrudpController<E ,T extends IService<E>> {

    @SuppressWarnings("SpringJavaAutowiredMembersInspection")
    @Autowired
    public T service;


    public ResEntity resEntity =  new ResEntity();

    @GetMapping("/show")
    @ApiOperation(value = "无条件查询功能")
    public ResEntity showAll(){
        List<E> list = service.list();
        System.out.println(list.toString());
        return resEntity.OK(service.list());
    }
    @PostMapping("/insert")
    @ApiOperation(value = "添加功能")
    public ResEntity insertOne(@RequestBody E e){
        return  service.save(e)?resEntity.OK():resEntity.Failure("操作失败，请重试!");
    }
    @PutMapping("/updateById")
    @ApiOperation(value = "修改功能")
    public ResEntity updateById(@RequestBody E e){
        return service.updateById(e)?resEntity.OK():resEntity.Failure("操作失败，请重试!");
    }
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "通过ID删除一个功能")
    public ResEntity deleteById(@PathVariable Long id){
        return service.removeById(id)?resEntity.OK():resEntity.Failure("操作失败，请重试!");

    }
    @DeleteMapping("/deleteIds")
    @ApiOperation(value = "批量删除功能")
    public ResEntity deleteIds(@RequestParam List<Serializable> ids){
        return service.removeBatchByIds(ids)?resEntity.OK():resEntity.Failure("操作失败，请重试!");
    }
    @GetMapping("/page1")
    @ApiOperation(value = "分页功能")
    public ResPageEntity page(@RequestParam int page, @RequestParam int limit){
        IPage<E> p = new Page<>(page,limit);
        service.page(p);
        return new ResPageEntity(p);
    }

    @DeleteMapping("/delids")
    @ApiOperation(value = "多选删除功能")
    public ResEntity deleteById(@RequestParam List<Serializable> ids) {
        return service.removeBatchByIds(ids) ? new ResEntity().OK() : new ResEntity().Failure();
    }

    @GetMapping("/page")
    public ResPageEntity page(int page , int limit, HttpServletRequest request){

        //获得当前请求里面所有参数 page=1 limit =10 uaccunt=a
        Map<String, String[]> map = request.getParameterMap();
        Set<Map.Entry<String, String[]>> keys = map.entrySet();

        IPage<E> ip =new Page<>(page,limit);
        QueryWrapper<E> qw = null;
        for(Map.Entry<String, String[]> e:keys){
            if(!e.getKey().equals("page") && !e.getKey().equals("limit")
                    && e.getValue().length > 0 && !e.getValue()[0].equals("")){
                qw = (qw == null ? new QueryWrapper<>():qw);
                qw.like(e.getKey(),e.getValue()[0]);
            }
        }


        return new ResPageEntity(ip);
    }

}
